/**
 * DocuemntDeleteTest.java 1.0 2007-6-9 上午01:19:17,创建 by yangfan
 */


package lia.ch1;

import static org.junit.Assert.*;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.junit.Test;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-9 上午01:19:17,创建
 */
public class DocuemntDeleteTest extends BaseIndexingTestCase
{
	@Override
	public void setUp() throws Exception
	{
		super.setUp();
	}

	public void testDeleteBeforeIndexMerge() throws IOException
	{
		IndexReader reader = IndexReader.open(dir);

		assertEquals(2, reader.maxDoc());//下一个Document的编号是2
		assertEquals(2, reader.numDocs());//索引的文档数量
		reader.deleteDocument(1);
		assertTrue(reader.isDeleted(1));
		assertTrue(reader.hasDeletions());
		assertEquals(2, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();

		reader = IndexReader.open(dir);
		assertEquals(2, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();
	}

	public void testDeleteAfterIndexMerge() throws IOException
	{
		IndexReader reader = IndexReader.open(dir);

		assertEquals(2, reader.maxDoc());//下一个Document的编号是2
		assertEquals(2, reader.numDocs());//索引的文档数量
		reader.deleteDocument(1);
		assertTrue(reader.isDeleted(1));
		assertTrue(reader.hasDeletions());
		assertEquals(2, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();

		IndexWriter writer = new IndexWriter(dir,getAnalyzer(),false);
		writer.optimize();
		writer.close();

		reader = IndexReader.open(dir);
		assertEquals(1, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();
	}

	@Test
	public void testDeleteByTerm() throws IOException
	{
		IndexReader reader = IndexReader.open(dir);

		assertEquals(3, reader.maxDoc());//下一个Document的编号是2
		assertEquals(3, reader.numDocs());//索引的文档数量
//		reader.deleteDocument(1);
		reader.deleteDocuments(new Term("city","Venice"));
		assertTrue(reader.isDeleted(1));
		assertTrue(reader.isDeleted(2));
		assertTrue(reader.hasDeletions());
		assertEquals(3, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();

		IndexWriter writer = new IndexWriter(dir,getAnalyzer(),false);
		writer.optimize();
		writer.close();

		reader = IndexReader.open(dir);
		assertEquals(1, reader.maxDoc());
		assertEquals(1, reader.numDocs());

		reader.close();
	}

	@Override
	protected Analyzer getAnalyzer()
	{
		return new SimpleAnalyzer();
	}

}
